<?php
namespace Admin\Controller;
use \Frame\Libs\BaseController;
use \Admin\Model\UserModel;

final class UserController extends BaseController
{
	public function index()
	{
        //验证是否登录
        $this->denyAccess();
		$modelObj = UserModel::getInstance();
		$arr = $modelObj->fetchAll();
		$this->smarty->assign("arr",$arr);
		$this->smarty->display("User/index.html");
	}
	public function del()
	{
        //验证是否登录
        $this->denyAccess();
		$id = $_GET["id"];
		$modelObj = UserModel::getInstance();
		if($modelObj->del($id)){
			$this->jump("id={$id}用户删除成功","?c=user");
		}else{
			$this->jump("id={$id}用户删除失败","?c=user");
		}
	}
	public function add()
	{
        //验证是否登录
        $this->denyAccess();
		$this->smarty->display("User/add.html");
	}
	public function insert()
	{
        //验证是否登录
        $this->denyAccess();
	    //设置时区
        date_default_timezone_set('PRC');

		$data["username"] = $_POST["username"];
		$data["password"] = md5($_POST["password"]);
        $data["name"] = $_POST["name"];
		$data["tel"] = $_POST["tel"];
        $data["addate"] = time();

        //判断两次输入的密码是否一致
        if($data["password"] != md5($_POST["comfirmpwd"])){
            $this->jump("两次输入的密码不一致","?c=User");
        }
        //创建模型类对象
		$modelOBJ = UserModel::getInstance();
        //判断用户是否已经注册
        if($modelOBJ->rowCount("username='{$data['username']}'")){
            $this->jump("用户名{$data['username']}已经被注册了!","?c=user");
        }
        if($modelOBJ->insert($data)){
            $this->jump("用户添加成功","?c=user");
        }else{
            $this->jump("用户添加失败","?c=user");
        }
	}

    public function login()
    {
        //显示登陆页面
        $this->smarty->display("User/login.html");
    }

    public function checkLogin()
    {
        //接收表达提交值
        $username = $_POST["username"];
        $password = md5($_POST["password"]);
        //$verify = $_POST["verify"];
        //echo $username."<bv>".$password;

        //判断验证码是否正确(不区分大小写)
        //if(strtolower($verify)!=strtolower($_SESSION["captcha"])){
        //    $this->jump("验证码不正确","?c=User&a=login");
        //}
        //判断用户的账号是否正确
        $modelOBJ = UserModel::getInstance();
        $arr = $modelOBJ->fetchOne("username='$username' and password='$password'");
        if(empty($arr)){
            $this->jump("账号密码错误","?c=user&a=login");
        }
		//更新用户的资料:最后登录的ip,最后登录时间,登录总次数
        $id              = $arr["id"];
        $last_login_ip   = $_SERVER['SERVER_ADDR'];
        $last_login_time = time();
        $login_times     = $arr["login_times"]+1;
        $result = $modelOBJ->edit("last_login_ip='$last_login_ip',last_login_time='$last_login_time',login_times='$login_times'",$id);
        if(!$result){
            $this->jump("用户信息更新失败","?c=user&a=login");
        }
		//将用户状态信息写入SESSION 静态初始化 开启SESSION
        $_SESSION["id"] = $arr["id"];
        $_SESSION["username"] = $username;
		//跳转到网站后台管理首页
        $this->jump("{$username}用户登录成功,正在跳转","?c=index&a=index");
    }
    //获取验证码
    public function captcha()
    {
        //创建验证码类的对象
        $captchaOBJ = new \Frame\Vendor\Captcha();
    }
    public function logout()
    {
        //删除SESSION数据
        unset($_SESSION["usrename"]);
        unset($_SESSION["id"]);
        //删除SESSION文件
        session_destroy();
        //删除SESION对应的COOKIE数据
        setcookie(session_name(),false);
        //跳转到后台登录页面
        $this->jump("用户退出成功","?c=User&a=login");
    }

}
